package com.ceo.common.utils;

/**
 * @author uto_dev_008
 * @desc
 * @since 2021/11/24 14:04
 * Copyright (c)  All rights reserved
 */
public class StringKit {


    /**
     * 把数组转换成字符串，并且以分隔符（decollator） 分割开,
     *
     * @param strs       对象数组
     * @param decollator 分割符
     * @return 分割后的字符串
     */
    public static String arrayToString(Object[] strs, String decollator) {
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < strs.length; i++) {
            str.append(strs[i] + decollator);
        }
        if (str.length() > 0) {
            str.deleteCharAt(str.length() - decollator.length());
        }
        return str.toString();
    }

    /**
     * 把object数组转换成String，如 id[]：1,2,3 转换为'1','2','3',
     * 数组中的空对象或空字符串将被剔除
     *
     * @param objs
     * @return 分割后的字符串
     */
    public static String arrayIDToString(Object[] objs) {
        int num = 0;
        StringBuffer string = new StringBuffer();
        for (Object obj : objs) {
            if (isBlank(obj))
                continue;
            String temp = obj.toString();
            temp = temp.trim().replace("'", "").replace("\"", "");
            if (num == 0) {
                string.append("'" + temp + "'");
                num++;
            } else
                string.append(",'" + temp + "'");
        }
        return string.toString();

    }

    /**
     * 判断字符串为 null 或者为  "" 时返回 true
     */
    public static boolean isBlank(Object str) {
        return str == null || "".equals(str.toString().trim()) || "null".equalsIgnoreCase(str.toString().trim()) ? true : false;
    }

    /**
     * 将字符串处理成适合in查询的条件(如:aa,bb,cc 处理成'aa','bb','cc')
     *
     * @param stringByComma
     * @return
     */
    public static String change_in(String stringByComma) {
        return change_in(stringByComma, ",");
    }

    /**
     * 将字符串处理成适合in查询的条件(如:aa;bb;cc 处理成'aa','bb','cc')
     *
     * @param stringByComma 处理的字符串
     * @param decollator    分割字符串
     * @return
     */
    public static String change_in(String stringByComma, String decollator) {
        if (stringByComma == null || stringByComma.equals(""))
            return "''";
        String[] strings = stringByComma.trim().split(decollator);
        return arrayIDToString(strings);
    }

    /***
     * 驼峰命名转为下划线命名
     *
     * @param para
     *        驼峰命名的字符串
     */
    public static String HumpToUnderline(String para) {
        StringBuilder sb = new StringBuilder(para);
        int temp = 0;//定位
        if (!para.contains("_")) {
            for (int i = 0; i < para.length(); i++) {
                if (Character.isUpperCase(para.charAt(i))) {
                    sb.insert(i + temp, "_");
                    temp += 1;
                }
            }
        }
        return sb.toString().toLowerCase();
    }

    /**
     * 字符串分割工具
     */
    public static String getKey(String field, Integer offset) {
        String[] fieldSplit = field.split("\\.");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < fieldSplit.length - offset; i++) {
            if (sb.length() > 0) {
                sb.append(".");
            }
            sb.append(fieldSplit[i]);
        }
        return sb.toString();
    }

    public static String getRightKey(String field, int offset) {
        String[] fieldSplit = field.split("\\.");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < fieldSplit.length ; i++) {
            if (i<offset){
                continue;
            }

            if (sb.length() > 0) {
                sb.append(".");
            }
            sb.append(fieldSplit[i]);
        }
        return sb.toString();

    }
}
